unit USelectdate;

interface

uses
  Windows, Messages,  Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, Calendar, ExtCtrls,System.SysUtils;

type
  TfrmSelectDate = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Calendar1: TCalendar;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    LabDate: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    SpeedButton5: TSpeedButton;
    labToday: TLabel;
    procedure Calendar1Change(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure Calendar1DblClick(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
  private
    { Private declarations }
    FFormatStr : String;
    Procedure SetFormatString(Const Values:string);
  public
    { Public declarations }
  published
   Property FormatStr : String read FFormatStr write SetFormatString;
  end;

function SelectDate(FDate:TDate;FormatStr:String='YYYY-MM-DD'): String;

var
  frmSelectDate: TfrmSelectDate;

implementation

{$R *.dfm}

function SelectDate(FDate:TDate;FormatStr:String='YYYY-MM-DD'): String;
begin

  Result := FormatDateTime(FormatStr, now());
  with TfrmSelectDate.Create(nil) do
  begin
    FormatStr := FFormatStr;
    if FDate <> 0 then
    begin
      Calendar1.CalendarDate := FDate;
      Result := FormatDateTime(FormatStr, FDate);
    end
    else
    begin
      Calendar1.CalendarDate := Date;
      Result := FormatDateTime(FFormatStr, now());
    end;
    labToday.Caption := FormatDateTime(FFormatStr, Date);

    if ShowModal = mrOK then
      Result := FormatDateTime(FFormatStr, Calendar1.CalendarDate)
    else
      Abort;
  end;
end;


procedure TfrmSelectDate.Calendar1Change(Sender: TObject);
begin
  LabDate.Caption:=FormatDateTime(FFormatStr, Calendar1.CalendarDate);
end;

procedure TfrmSelectDate.SpeedButton4Click(Sender: TObject);
begin
  Calendar1.PrevMonth;
end;

procedure TfrmSelectDate.SpeedButton1Click(Sender: TObject);
begin
  Calendar1.PrevYear;
end;

procedure TfrmSelectDate.SpeedButton3Click(Sender: TObject);
begin
  Calendar1.NextMonth;
end;

procedure TfrmSelectDate.SpeedButton2Click(Sender: TObject);
begin
  Calendar1.NextYear;
end;

procedure TfrmSelectDate.Calendar1DblClick(Sender: TObject);
begin
  Self.ModalResult:=mrOK;
end;

procedure TfrmSelectDate.SpeedButton5Click(Sender: TObject);
begin
  Calendar1.CalendarDate:=Date;
end;

procedure TfrmSelectDate.SetFormatString(const Values: string);
begin
  if FFormatStr<>Values then
    FFormatStr:=Values;
end;

end.
